// pages/bind/bind.js

const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    student_list:[],
    ismask:false,
    student_id: '',
    pass_word: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    const {member_id} = options;
    that.getStudentBindList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  getStudentBindList: function (){
    const {member_id} = wx.getStorageSync('authinfo');

    App._post('/api/login/Student/getStudentBindList', {pid:member_id}, (res) => {
      let data = res.data.data;
      let that = this;
      that.setData({
        student_list:data
      })
    })
  },

  show_bind:function (){
    this.setData({
      ismask: true,
    })
  },
  hide_bind:function (){
    this.setData({
      ismask: false,
    })
  },
  setBindStudent:function (){
    let that = this;
    console.log(that.data.student_id);
    console.log(that.data.pass_word);

    const {student_id,pass_word} =  that.data;

    App._post(
        '/api/login/Student/studentNoBind',
        {student_no:student_id,pwd:pass_word},
        (res) => {
          const {msg,data,code} = res.data;

          if (code === 200){
            that.hide_bind();
            App.showSuccess(msg,Function);
          }else{
            App.showWarn(msg,Function);
          }
        })
  },

  switchStudent:function (e){
    const {openid} = e.currentTarget.dataset
    // wx.setStorageSync('token', openid);
    wx.setStorage({
      key: "token",
      data: openid,
      encrypt: false, // 若开启加密存储，setStorage 和 getStorage 需要同时声明 encrypt 的值为 true
      success() {
        wx.reLaunch({
          url: '/pages/user/user'
        })
      }
    })

  },
})